In [46]:
# 上載手寫辨識數據
# sklearn.datasets.load_digits
from sklearn import datasets
mnist = datasets.load_digits()
In [50]:
mnist.keys()
Out[50]:
dict_keys(['data', 'target', 'target_names', 'images', 'DESCR'])
In [81]:
data = mnist.images
target = mnist.target.reshape(-1,1)
data.shape , target.shape
Out[81]:
((1797, 8, 8), (1797, 1))
In [82]:
%matplotlib inline
index = 3
import matplotlib.pyplot as plt
plt.figure(figsize=(1,1))
plt.imshow(data[index], cmap='gray_r')
plt.axis('off')
plt.show()
In [83]:
# 檢查 X 與 y
index = 0
print(data[index])
print('-'*35)
print(mnist.target[index])
print('-reshape-')
print(data[index].reshape(-1)) # .ravel() , .flatten()
[[ 0.  0.  5. 13.  9.  1.  0.  0.]
 [ 0.  0. 13. 15. 10. 15.  5.  0.]
 [ 0.  3. 15.  2.  0. 11.  8.  0.]
 [ 0.  4. 12.  0.  0.  8.  8.  0.]
 [ 0.  5.  8.  0.  0.  9.  8.  0.]
 [ 0.  4. 11.  0.  1. 12.  7.  0.]
 [ 0.  2. 14.  5. 10. 12.  0.  0.]
 [ 0.  0.  6. 13. 10.  0.  0.  0.]]
-----------------------------------
0
-reshape-
[ 0.  0.  5. 13.  9.  1.  0.  0.  0.  0. 13. 15. 10. 15.  5.  0.  0.  3.
 15.  2.  0. 11.  8.  0.  0.  4. 12.  0.  0.  8.  8.  0.  0.  5.  8.  0.
  0.  9.  8.  0.  0.  4. 11.  0.  1. 12.  7.  0.  0.  2. 14.  5. 10. 12.
  0.  0.  0.  0.  6. 13. 10.  0.  0.  0.]
In [98]:
# 分類 mnist 分十類別  [0....9]
from sklearn.model_selection import train_test_split
X = data.reshape(1797,64)
y = target.flatten()

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
In [99]:
# 套模型
# hyper-parameter <-- GridSearchCV 

from sklearn.linear_model import LogisticRegression

clf = LogisticRegression(random_state=0, solver='newton-cg', 
                         multi_class='multinomial')
clf.fit(X_train, y_train)
 
Out[99]:
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, max_iter=100, multi_class='multinomial',
          n_jobs=None, penalty='l2', random_state=0, solver='newton-cg',
          tol=0.0001, verbose=0, warm_start=False)
In [101]:
# 評分
print('X_train ',  clf.score(X_train , y_train) )
print('X_test ',   clf.score(X_test , y_test)   )
X_train  1.0
X_test  0.9722222222222222
In [ ]:
 
In [144]:
# 找出那些沒有預測正確
import numpy as np
import matplotlib.pyplot as plt

y_pred = clf.predict(X_test) 
diff = (y_pred != y_test)
print('Ground Truth', y_test[diff])
print('Predictive  ', y_pred[diff])
for i in np.arange(X_test.shape[0])[diff]:
    plt.figure(figsize=(1,1))
    plt.axis('off')    
    plt.imshow(X_test[i].reshape(8,8) ,  cmap='gray')    
    plt.title( str(y_test[i])+ '-->' + str(y_pred[i]) )
    plt.show()    
Ground Truth [9 7 3 9 5 4 5 8 6 5]
Predictive   [3 5 5 8 2 1 9 5 5 6]
In [125]:
X_test.shape
Out[125]:
(360, 64)
In [ ]:
 
In [ ]:
 
In [ ]:
 
In [26]:
from skimage import io
import matplotlib.pyplot as plt

my_image = io.imread('number3.jpg', as_gray=True)

# look at the image
print(my_image.shape)
plt.imshow(my_image, cmap = 'gray')
(72, 72)
Out[26]:
<matplotlib.image.AxesImage at 0x2380c739c50>
In [43]:
# Identifying Malaria In Blood Imagery 
from skimage import io
import matplotlib.pyplot as plt

my_image = io.imread('00000.jpg', as_gray=True)

# look at the image
print(my_image.shape)
plt.imshow(my_image)
(128, 128)
Out[43]:
<matplotlib.image.AxesImage at 0x2380db97400>
In [45]:
#print(list(my_image.flatten()))
In [ ]:
 
In [35]:
# Identifying Accents in Spectrograms of Speech
from skimage import io
import matplotlib.pyplot as plt
my_image = io.imread('10000.png', as_gray=True)
# look at the image
print(my_image.shape)
plt.imshow(my_image)
(128, 173)
Out[35]:
<matplotlib.image.AxesImage at 0x2380da5a550>
In [36]:
my_image
Out[36]:
array([[0.        , 0.        , 0.        , ..., 0.        , 0.        ,
        0.        ],
       [0.07058824, 0.05882353, 0.04313725, ..., 0.13333333, 0.15686275,
        0.15294118],
       [0.10196078, 0.10196078, 0.08235294, ..., 0.16470588, 0.17254902,
        0.17254902],
       ...,
       [0.26666667, 0.31764706, 0.26666667, ..., 0.59607843, 0.52156863,
        0.5254902 ],
       [0.27843137, 0.2627451 , 0.29411765, ..., 0.4627451 , 0.48627451,
        0.4745098 ],
       [0.23529412, 0.2       , 0.21568627, ..., 0.39607843, 0.38823529,
        0.40392157]])